package com.yeyks.rabbitmq.send;

import com.yeyks.rabbitmq.queue.DelayRabbitMqQueue;
import com.yeyks.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author fuchangshuai
 * @date 2019/4/17 0017
 */
@Component
@Slf4j
public class DelayQueueMsg {
    @Autowired
    private AmqpTemplate template;
    //单位/毫秒
    private static final Integer expiration = 1000 * 10;

    //参数1：exchange：接受消息提供者（生产者）的消息，并根据消息的RoutingKey和Exchange绑定的BindingKey分配消息
    //参数2：RoutingKey：指定当前消息被谁接受
    //参数3：接收到的消息
    //参数4：MessagePostProcessor接口的实现（java8的写法）
    public void sendDelay(String msg) {
        log.info("发消息的时间：" + DateUtils.format(new Date()));
        template.convertAndSend(DelayRabbitMqQueue.ORDER_DELAY_EXCHANGE,
                DelayRabbitMqQueue.ORDER_DELAY_BINDING_KEY, msg, message -> {
                    message.getMessageProperties().setExpiration(String.valueOf(expiration));
                    return message;
                });
    }

}
